# -*- coding:utf-8 -*-

from VHDLine_ui import Ui_MainWindow
from PySide import QtGui, QtCore

import infors, os

class StyledDelegate(QtGui.QStyledItemDelegate):
	def paint(self, painter, option, index):
		view_option = QtGui.QStyleOptionViewItem(option)
		if view_option.state & QtGui.QStyle.State_HasFocus:
			view_option.state = view_option.state ^ QtGui.QStyle.State_HasFocus
		QtGui.QStyledItemDelegate.paint(self, painter, view_option, index)

class VHDLine_MainWindow(QtGui.QMainWindow):
	def __init__(self, parent=None):
		QtGui.QMainWindow.__init__(self, parent)
		self.ui = Ui_MainWindow()
		self.file_list = []
		self.file_path_list = []
		
		self.ui.setupUi(self)
		self.setWindowTitle("VHDLine %s" % (infors.version_string))
		self.init_fileTable()
		self.fileTable_styled_delegate = StyledDelegate()
		self.ui.fileTable.setItemDelegate(self.fileTable_styled_delegate)
		
		self.ui.softwareNameLabel.installEventFilter(self)
		
	def eventFilter(self, obj, event):
		if obj == self.ui.softwareNameLabel and event.type() == QtCore.QEvent.MouseButtonPress:
			self.show_about()
			return True
		else:
			return False
		
	def show_about(self):
		QtGui.QMessageBox.information(self,
                "关于VHDLine", infors.about_message)
		
	def closeEvent(self, event):
		reply = QtGui.QMessageBox.question(self, "VHDLine", 
				infors.exit_warning,
				QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
		if reply == QtGui.QMessageBox.Yes:
			event.accept()
		else:
			event.ignore()
			
	def init_fileTable(self):
		self.ui.fileTable.reset()
		self.update_fileTable()
		
	def update_fileTable(self):
		rows_new = len(self.file_list)
		rows_old = self.ui.fileTable.rowCount()
		for i in range(rows_old,rows_new):
			self.ui.fileTable.insertRow(i)
			self.ui.fileTable.setRowHeight(i, 20)
			for j in range(6):
				item = QtGui.QTableWidgetItem(str(self.file_list[i][j+1]))
				self.ui.fileTable.setItem(i, j, item)
			self.fileTable_color_row(i)
		
	def fileTable_color_row(self, row):
		cols = 	self.ui.fileTable.columnCount()
		if not self.file_list[row][7]:
			for i in range(cols):
				self.ui.fileTable.item(row, i).setBackground(QtGui.QColor(infors.item_not_counted_color))
		else:
			for i in range(cols):
				self.ui.fileTable.item(row, i).setBackground(QtGui.QColor(infors.item_counted_color))
				
	@QtCore.Slot(QtCore.QModelIndex)
	def on_fileTable_entered(self, index):
		if not index.isValid():
			return 
		row = index.row()
		tooltip = infors.fileinfo_on_tolltip % (self.file_list[row][1], os.path.dirname(self.file_list[row][0]))
		if self.file_list[row][7]:
			tooltip = "已统计" + tooltip
		else:
			tooltip = "未统计" + tooltip
		QtGui.QToolTip.showText(QtGui.QCursor.pos(), tooltip)
	
	@QtCore.Slot(int, int)
	def on_fileTable_cellDoubleClicked(self, row, col):
		pass
	
	@QtCore.Slot()
	def on_addFileButton_clicked(self):
		fileName = QtGui.QFileDialog.getOpenFileName(self, 
											"选择文件", os.getcwd(), 
											"所有文件 (*.*)", "",
											QtGui.QFileDialog.Options() | QtGui.QFileDialog.DontUseNativeDialog)
		if os.path.isfile(fileName[0]):
			if fileName[0] in self.file_path_list:
				QtGui.QMessageBox.warning(self, "VHDLine", 
										infors.file_exist_warning % fileName[0], 
										QtGui.QMessageBox.Yes)
			else:
				self.file_list.append([fileName[0], 
									os.path.basename(fileName[0]), 
									0, 0, 0, 0, 0, False])
				# 文件路径，文件名，文件行数，代码行数，注释行数，空白行数，注释率，是否统计
				self.file_path_list.append(fileName[0])
				self.update_fileTable()
		
	@QtCore.Slot()
	def on_addPathButton_clicked(self):
		print("Add Directory Clicked.")
		
	@QtCore.Slot()
	def on_configButton_clicked(self):
		print("Config Clicked.")
		
	@QtCore.Slot()
	def on_startStaticButton_clicked(self):
		print("Start static Clicked.")
		
	@QtCore.Slot()
	def on_saveResaultButton_clicked(self):
		print("Save resault Clicked.")
		
	@QtCore.Slot()
	def on_exitButton_clicked(self):
		self.close()

